import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import {longLimtVal, rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import {list} from "@/views/baseinfo/person/SsPerson.api";
//列表数据
export const columns: BasicColumn[] = [
   {
    title: '卡号',
    align:"center",
    dataIndex: 'cardNumber',
     fixed:'left'
   },
  {
    title:"持卡人姓名",
    align:"center",
    dataIndex:"personName",
     fixed:'left'
  },
  {
    title:"持卡人学/工号",
    align:"center",
    dataIndex:"personCode",
    fixed:'left'
  },
  {
    title:"所属部门",
    align:"center",
    dataIndex:"personDept",
  },
  // {
  //   title:"持卡人员类型",
  //   align:"center",
  //   dataIndex:"personType",
  //   customRender: ({ text }) => {
  //     return render.renderDict(text, 'personType');
  //   },
  // },
  {
    title: '卡状态',// 255-空白 0-激活 1-挂失 2-注销',
    align:"center",
    dataIndex: 'cardStatus',
    customRender: ({ text }) => {
      return render.renderDict(text, 'cardStatus');
    },
  },

/*   {
    title: '学/工号',
    align:"center",
    dataIndex: 'personId'
   },*/
   {
    title: '开始时间',
    align:"center",
    dataIndex: 'startDate'
   },
   {
    title: '结束时间',
    align:"center",
    dataIndex: 'endDate'
   },

  //  {
  //   title: '备注',
  //   align:"center",
  //   dataIndex: 'description'
  //  },
/*   {
    title: '有效标志位',
    align:"center",
    dataIndex: 'validFlag'
   },*/
  // {
  //   title: '卡密码',
  //   align:"center",
  //   dataIndex: 'cardPassword'
  // },
  // {
  //   title: '卡类型',// 0-普通卡 1-VIP卡  -1-未知卡类型',
  //   align:"center",
  //   dataIndex: 'cardType',
  //   customRender: ({ text }) => {
  //     return render.renderDict(text, 'cardType');
  //   },
  // },

   {
    title: '卡介质',// 0-IC卡 1-有源RFID 2-CPU卡 3 身份证卡',
    align:"center",
    dataIndex: 'category',
     customRender: ({ text }) => {
       return render.renderDict(text, 'card_category');
     },
   },
  //  {
  //   title: '是否虚拟卡',// 1为虚拟卡',
  //   align:"center",
  //   dataIndex: 'isVirtual',
  //    customRender: ({ text }) => {
  //      return render.renderDict(text, 'card_isVirtual');
  //    },
  //  },
/*   {
    title: '数据更新时间，数据补偿使用',
    align:"center",
    dataIndex: 'recordUpdateTime'
   },*/
];
//查询数据
export const searchFormSchema: FormSchema[] = [
	{
      label: "卡号",
      field: 'cardNumber',
      component: 'Input',
      //colProps: {span: 6},
 	},
  {
    label:"持卡人学/工号",
    component:"Input",
    field:"personCode",
  },
  {
    label:"所属部门",
    component:"JSelectDept",
    field:"personDept",
    componentProps:{
      checkStrictly:false,
      multiple:false
    }
  },
  {
    label:"持卡人姓名",
    component:"Input",
    field:"personName",
  },
  {
    label: '卡状态',// 255-空白 0-激活 1-挂失 2-注销',
    field: 'cardStatus',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"cardStatus",
    },
    
  },
  {
    label: '卡介质',// 0-IC卡 1-有源RFID 2-CPU卡 3 身份证卡',
    field: 'category',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"card_category",
    }
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '卡号',
    field: 'cardNumber',
    component: 'Input',
    dynamicRules: ({ values }) => {
      console.log('values:', values);
      //需要return
      return [
        {
          //默认开启表单检验
          required: true,
          // value 当前卡号输入的值
          validator: (_, value) => {
            //需要return 一个Promise对象
            return new Promise((resolve, reject) => {
              if (!value) {
                reject('请输入卡号！');
              }
              //验证卡号是否正确
              let reg = /^[a-zA-Z0-9]{10}$/;
              if (!reg.test(value)) {
                reject('请输入10位字母或者数字卡号！');
              }
              resolve();
            });
          },
        },
      ];
    },

    slot:"cardNumberSlot",
  },
  {
    label:"持卡人姓名",
    component:"Input",
    field:"personName",
    dynamicDisabled:true,
    show:false,
  },
  {
    label:"持卡人学/工号",
    component:"Input",
    field:"personCode",
    dynamicDisabled:true,
  },
  {
    label:"所属部门",
    component:"Input",
    field:"personDept",
    dynamicDisabled:true,
  },
  // {
  //   label:"持卡人员类型",
  //   component: 'JDictSelectTag',
  //   componentProps:{
  //     dictCode:"personType",
  //   },
  //   field:"personType",
  //   dynamicDisabled:true,
  // },
  // {
  //   label: '卡类型',// 0-普通卡 1-VIP卡  -1-未知卡类型',
  //   field: 'cardType',
  //   component: 'JDictSelectTag',
  //   componentProps:{
  //     dictCode:"cardType",
  //     stringToNumber: true,
  //   },
  //   defaultValue:0,
  // },
  {
    label: '卡密码',
    field: 'cardPassword',
    component: 'Input',
    defaultValue:123456,
    dynamicRules: ({model,schema}) => {
      return [
        {
          validator: (_, value) => {
            return longLimtVal(value,20)
          },
        },
      ];
    },
    show:false,
  },
  {
    label: '人员',
    field: 'personId',
    component: 'JSearchSelect',
    componentProps: ({ formModel, formActionType }) => {  //formModel, formActionType
      console.log('formModel:', formModel);
      return {
        dict:"ss_person where status != 0,name,id",
        pageSize: 5,
        async: true,
        onChange: (e: any) => {
          console.log(e);
          console.log(formActionType);
          console.log(formModel);
          if(!e){
              formModel.personName = "";
              formModel.personCode = "";
              formModel.personDept = "";
              formModel.personType = "";
              return;
          }
          let params = {id:e};
          console.log(params);
          let res =  list(params);
          res.then((data)=>{
            console.log(data);
            if(data.records?.length>0){
            formModel.personName = data.records[0].name;
            formModel.personCode = data.records[0].code;
            formModel.personDept = data.records[0].departmentId_dictText;
            formModel.personType = data.records[0].personType;
            }

          })
        },
      };
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请输入选择人员!'},
      ];
    },
  },
  {
    label: '开始时间',
    field: 'startDate',
    component: 'DatePicker',
    componentProps: {
       showTime: true,
       valueFormat: 'YYYY-MM-DD HH:mm:ss',
     },
  },
  {
    label: '结束时间',
    field: 'endDate',
    component: 'DatePicker',
    componentProps: {
       showTime: true,
       valueFormat: 'YYYY-MM-DD HH:mm:ss'
     },
  },
  {
    label: '卡状态',// 255-空白 0-激活 1-挂失 2-注销',
    field: 'cardStatus',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"cardStatus",
      disabled:true
    },
    defaultValue:"255",
    show:({values})=>{
      return values.id
    }
    /* dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请选择卡状态!'},
      ];
    }, */
  },
  {
    label: '备注',
    field: 'description',
    component: 'InputTextArea',
    dynamicRules: ({model,schema}) => {
      return [
        {
          validator: (_, value) => {
            return longLimtVal(value,50)
          },
        },
      ];
    },
  },
/*  {
    label: '有效标志位',
    field: 'validFlag',
    component: 'Input',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入有效标志位!'},
          ];
     },
  },*/
  {
    label: '卡介质',// 0-IC卡 1-有源RFID 2-CPU卡 3 身份证卡',
    field: 'category',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"card_category",
    },
    defaultValue:"0",
  },
  {
    label: '是否虚拟卡',// 1为虚拟卡',
    field: 'isVirtual',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"card_isVirtual",
      stringToNumber: true,
    },
    defaultValue:0,
  },
/*  {
    label: '数据更新时间，数据补偿使用',
    field: 'recordUpdateTime',
    component: 'DatePicker',
    componentProps: {
       showTime: true,
       valueFormat: 'YYYY-MM-DD HH:mm:ss'
     },
  },*/
	// TODO 主键隐藏字段，目前写死为ID
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
];




export const formSchemaByPerson: FormSchema[] = [
  {
    label: '卡号',
    field: 'cardNumber',
    component: 'Input',
    dynamicRules: ({ values }) => {
      console.log('values:', values);
      //需要return
      return [
        {
          //默认开启表单检验
          required: true,
          // value 当前卡号输入的值
          validator: (_, value) => {
            //需要return 一个Promise对象
            return new Promise((resolve, reject) => {
              if (!value) {
                reject('请输入卡号！');
              }
              //验证卡号是否正确
              let reg = /^[a-zA-Z0-9]{10}$/;
              if (!reg.test(value)) {
                reject('请输入10位字母或者数字卡号！');
              }
              resolve();
            });
          },
        },
      ];
    },

    slot:"cardNumberSlot",
  },
  {
    label:"持卡人姓名",
    component:"Input",
    field:"personName",
    dynamicDisabled:true,
    show:false,
  },
  {
    label:"持卡人学/工号",
    component:"Input",
    field:"personCode",
    dynamicDisabled:true,
  },
  {
    label:"所属部门",
    component:"Input",
    field:"personDept",
    dynamicDisabled:true,
  },
  // {
  //   label:"持卡人员类型",
  //   component: 'JDictSelectTag',
  //   componentProps:{
  //     dictCode:"personType",
  //   },
  //   field:"personType",
  //   dynamicDisabled:true,
  // },
  // {
  //   label: '卡类型',// 0-普通卡 1-VIP卡  -1-未知卡类型',
  //   field: 'cardType',
  //   component: 'JDictSelectTag',
  //   componentProps:{
  //     dictCode:"cardType",
  //     stringToNumber: true,
  //   },
  //   defaultValue:0,
  // },
  {
    label: '卡密码',
    field: 'cardPassword',
    component: 'Input',
    defaultValue:123456,
    dynamicRules: ({model,schema}) => {
      return [
        {
          validator: (_, value) => {
            return longLimtVal(value,20)
          },
        },
      ];
    },
    show:false,
  },
  {
    label: '人员',
    field: 'personId',
    component: 'JSearchSelect',
    componentProps: ({ formModel, formActionType }) => {  //formModel, formActionType
      console.log('formModel:', formModel);
      return {
        dict:"ss_person where status != 0,name,id",
        pageSize: 5,
        async: true,
        onChange: (e: any) => {
          console.log(e);
          console.log(formActionType);
          console.log(formModel);
          if(!e){
            formModel.personName = "";
            formModel.personCode = "";
            formModel.personDept = "";
            formModel.personType = "";
            return;
          }
          let params = {id:e};
          console.log(params);
          let res =  list(params);
          res.then((data)=>{
            console.log(data);
            if(data.records?.length>0){
              formModel.personName = data.records[0].name;
              formModel.personCode = data.records[0].code;
              formModel.personDept = data.records[0].departmentId_dictText;
              formModel.personType = data.records[0].personType;
            }

          })
        },
      };
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请输入选择人员!'},
      ];
    },
    dynamicDisabled:true,
  },
  {
    label: '开始时间',
    field: 'startDate',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    label: '结束时间',
    field: 'endDate',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
  },
  {
    label: '卡状态',// 255-空白 0-激活 1-挂失 2-注销',
    field: 'cardStatus',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"cardStatus",
      disabled:true
    },
    defaultValue:"255",
    show:({values})=>{
      return values.id
    }
    /* dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请选择卡状态!'},
      ];
    }, */
  },
  {
    label: '备注',
    field: 'description',
    component: 'InputTextArea',
    dynamicRules: ({model,schema}) => {
      return [
        {
          validator: (_, value) => {
            return longLimtVal(value,50)
          },
        },
      ];
    },
  },
  /*  {
      label: '有效标志位',
      field: 'validFlag',
      component: 'Input',
      dynamicRules: ({model,schema}) => {
            return [
                   { required: true, message: '请输入有效标志位!'},
            ];
       },
    },*/
  {
    label: '卡介质',// 0-IC卡 1-有源RFID 2-CPU卡 3 身份证卡',
    field: 'category',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"card_category",
    },
    defaultValue:"0",
  },
  {
    label: '是否虚拟卡',// 1为虚拟卡',
    field: 'isVirtual',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"card_isVirtual",
      stringToNumber: true,
    },
    defaultValue:0,
  },
  /*  {
      label: '数据更新时间，数据补偿使用',
      field: 'recordUpdateTime',
      component: 'DatePicker',
      componentProps: {
         showTime: true,
         valueFormat: 'YYYY-MM-DD HH:mm:ss'
       },
    },*/
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
];


// 高级查询数据
export const superQuerySchema = {
  cardNumber: {title: '卡号',order: 0,view: 'text', type: 'string',},
  cardType: {title: '卡类型 0-普通卡 1-VIP卡  -1-未知卡类型',order: 1,view: 'number', type: 'number',},
  cardPassword: {title: '卡密码',order: 2,view: 'text', type: 'string',},
  personId: {title: '学/工号',order: 3,view: 'number', type: 'number',},
  startDate: {title: '开始时间',order: 4,view: 'datetime', type: 'string',},
  endDate: {title: '结束时间',order: 5,view: 'datetime', type: 'string',},
  cardStatus: {title: '卡状态 255-空白 0-激活 1-挂失 2-注销',order: 6,view: 'text', type: 'string',},
  description: {title: '备注',order: 7,view: 'text', type: 'string',},
  validFlag: {title: '有效标志位',order: 8,view: 'text', type: 'string',},
  category: {title: '卡介质 0-IC卡 1-有源RFID 2-CPU卡 3 身份证卡',order: 9,view: 'text', type: 'string',},
  isVirtual: {title: '区分是否是虚拟卡 1为虚拟卡',order: 10,view: 'number', type: 'number',},
  recordUpdateTime: {title: '数据更新时间，数据补偿使用',order: 11,view: 'datetime', type: 'string',},
};

/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData): FormSchema[]{
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
